package spring.superrun.record.mvc.service.impl;

import spring.superrun.record.mvc.dao.SuperrunLocationsDao;
import spring.superrun.record.mvc.dto.LocationVerityDto;
import spring.superrun.record.mvc.entity.SuperrunLocations;
import spring.superrun.record.mvc.entity.SuperrunRunrecords;
import spring.superrun.record.mvc.dao.SuperrunRunrecordsDao;
import spring.superrun.record.mvc.entity.enumlibs.ServiceResultCode;
import spring.superrun.record.mvc.entity.response.ServiceResultOfT;
import spring.superrun.record.mvc.service.SuperrunRunrecordsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * (SuperrunRunrecords)表服务实现类
 *
 * @author makejava
 * @since 2021-06-18 09:14:33
 */
@Service("superrunRunrecordsService")
public class SuperrunRunrecordsServiceImpl implements SuperrunRunrecordsService {


    @Resource
    private SuperrunRunrecordsDao superrunRunrecordsDao;
    @Resource
    private SuperrunLocationsDao _locationDao;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SuperrunRunrecords queryById(String id) {
        return this.superrunRunrecordsDao.queryById(id);
    }

    @Override
    public SuperrunRunrecords queryByIdAndLocation(String id) {
        return superrunRunrecordsDao.queryByIdAndLocation(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<SuperrunRunrecords> queryAllByLimit(int offset, int limit) {
        return this.superrunRunrecordsDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param superrunRunrecords 实例对象
     * @return 实例对象
     */
    @Override
    public SuperrunRunrecords insert(SuperrunRunrecords superrunRunrecords) {
        this.superrunRunrecordsDao.insert(superrunRunrecords);
        return superrunRunrecords;
    }

    /**
     * 修改数据
     *
     * @param superrunRunrecords 实例对象
     * @return 实例对象
     */
    @Override
    public SuperrunRunrecords update(SuperrunRunrecords superrunRunrecords) {
        this.superrunRunrecordsDao.update(superrunRunrecords);
        return this.queryById(superrunRunrecords.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.superrunRunrecordsDao.deleteById(id) > 0;
    }

    @Override
    public ServiceResultOfT<LocationVerityDto>
    RunRecordExistsAndVerityIsNextByLocationIdAsync(String userId, String locationId) {
        ServiceResultOfT<LocationVerityDto> result = new ServiceResultOfT<>();
        result.setEntities(new LocationVerityDto());
        result.IsFailed("验证不通过失败");
        if (userId.isEmpty() && locationId.isEmpty())
        {
            result.IsFailed(ServiceResultCode.KeyIsNull,"用户id为空或位置id为空");
            return result;
        }
        //.stream().map(SuperrunRunrecords::getLocation).collect(Collectors.toList())
        List<SuperrunLocations> locations =
                _locationDao.queryAllByLimit(0,100);
        if (null==locations||locations.isEmpty()){
            result.IsFailed("记录失败,位置信息异常!!!");
            return result;
        }
        locations.sort((l1,l2)->l2.getOrderid().compareTo(l1.getOrderid()));
        //扫码得当前位置
        Optional<SuperrunLocations> location = locations.stream()
                .filter(q->q.getId().equals(locationId)).findFirst();
        if (null==location.get())
        {
            result.IsFailed("记录失败,位置信息异常!!!");
            return result;
        }
        List<SuperrunRunrecords> userRecords =
                superrunRunrecordsDao.queryRecordLocationByUserId(userId);
        //如果是第一个打卡点那就判断是否为起始点
        if (userRecords.size()==0&&!location.get().getOrderid().equals(0))
        {
            result.IsFailed("记录失败,请从起点开始!!!");
            return result;
        }
        userRecords.sort((q1,q2)->q2.getFinishtime().compareTo(q1.getFinishtime()));
        List<SuperrunRunrecords>mapRes=  userRecords.stream()
                .filter(s->s.getLocationid().equals(locationId)).collect(Collectors.toList());
        if (userRecords.size()>0&&mapRes.size()>0)
        {
            result.IsFailed("记录失败,该位置你已经打过卡!!!");
            return result;
        }
        SuperrunRunrecords userLastRecord = userRecords.size()>0?userRecords.get(0):null;
        //判断这个一个地点是否为这个地点
        if (null!=userLastRecord
                &&location.get().getOrderid()
                != userLastRecord.getLocation().getOrderid()+1)
        {
            Optional<SuperrunLocations> rollbackLocation = locations.stream()
                    .filter(q->q.getOrderid()
                            .equals(userLastRecord.getLocation().getOrderid()+1))
                    .findFirst();
            result.IsFailed("记录失败,请前往:“"
                    +rollbackLocation.get().getLocationname()+"”打卡位置再继续!!!");
            return result;
        }
        result.getEntities().setIsVerityAndLastDistance(true
                ,location.get().getLastdistance());
        result.IsSuccess("验证通过");
        return result;
    }

    @Override
    public List<SuperrunRunrecords> GetUserRecords(String userId) {
        return superrunRunrecordsDao.queryRecordLocationByUserId(userId);
    }
}
